/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package factory2;

import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author wida46984148
 */
public class PGame2 extends BasicGameState {
public static final int ID = 2;
    Image imatgeFons;
    StateBasedGame state;
    ArrayList<NauBase> anb = null;
    aux2 a = new aux2();
    float x = 350;
    float y = 800;
    boolean gamestart = false;
    boolean gameend = false;
    boolean gamepause = true;
    NauBase nbW = null;
    MoveShoot ms = null;
    MoveShoot ms2 = null;
    int id = 0;

    public void init(GameContainer container, StateBasedGame game)
            throws SlickException {
        this.state = game;
        imatgeFons = new Image("src/data/land2.jpg");
        if (!gamestart) {
            nbW = Naves.nausWida("nave2");
            a.posicionaUnitatAmiga(x, y, nbW);
            //nbW = null;
            gamestart = true;
        }
    }

    @Override
    public int getID() {
        return ID;
    }


    public void render(GameContainer gc, StateBasedGame game, Graphics g)
            throws SlickException {
        imatgeFons.draw(0, 0);
        a.dibuixar(g, gc);
//        p.dibuixar(g, gc);
//        mi.render(gc, g);
    }


    public void update(GameContainer gc, StateBasedGame game, int delta)
            throws SlickException {

        Input input = gc.getInput();
        if (y > 480) {
            y -= 5;
            nbW.setLocation(x, y);
        }
        if (gameend) {
            if (y > -150) {
                y -= 5;
                nbW.setLocation(x, y);
            } else {
                gc.exit();
            }
        } else {
            if (y > 480) {
                y -= 5;
                nbW.setLocation(x, y);
            }

//        if (input.isKeyPressed(Input.KEY_1)) {
//            nbW = Naves.nausWida("nave2");
//            a.posicionaUnitatAmiga(x, y, nbW);
//            ms = new MoveShoot(10, new Image("src/data/shot.jpg"));
//            id = 1;
//        }
//        if (input.isKeyPressed(Input.KEY_2)) {
//            nbW = Naves.nausWida("nave3");
//            a.posicionaUnitatAmiga(x, y, nbW);
//            ms = new MoveShoot(10, new Image("src/data/shot2.png"));
//            id = 2;
//        }
            if (input.isKeyDown(Input.KEY_A)) {
                float auxX = x;
                x += -0.4f * delta;
                if (x <= -30) {
                    x = auxX;
                }
                nbW.setLocation(x, y);
            }

            if (input.isKeyDown(Input.KEY_D)) {
                float auxX = x;
                x += 0.4f * delta;
                if (700 <= x) {
                    x = auxX;
                }
                nbW.setLocation(x, y);
            }
            if (input.isKeyDown(Input.KEY_S)) {
                float hip = 0.4f * delta;
                float auxY = y;
                x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                y += hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                if (480 <= y) {
                    y = auxY;
                }
                nbW.setLocation(x, y);
            }

            if (input.isKeyDown(Input.KEY_W)) {
                float hip = 0.4f * delta;
                float auxY = y;
                x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                y -= hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                if (y <= 0) {
                    y = auxY;
                }
                nbW.setLocation(x, y);
            }

            if (input.isKeyPressed(Input.KEY_SPACE)) {
//            if (id == 1) ms = new MoveShoot(10, new Image("src/data/shot.jpg"));
//            else if(id == 2) ms = new MoveShoot(10, new Image("src/data/shot2.png"));
                ms = new MoveShoot(10, new Image("src/data/shot.jpg"));
                a.posShoot(x + 65, y, ms);
                ms2 = new MoveShoot(10, new Image("src/data/shot.jpg"));
                a.posShoot(x + 51, y, ms2);
            }

            if (input.isKeyPressed(Input.KEY_5)) {
                game.enterState(2);
            }
//            if (gamepause) {
//                if (input.isKeyPressed(Input.KEY_P)) {
//                    gc.pause();
//                    gamepause = false;
//                }
//            } else {
//                if (input.isKeyPressed(Input.KEY_P)) {
//                    gc.resume();
//                    gamepause = true;
//                }
//            }

//            if (input.isKeyPressed(Input.KEY_P)) {
//                if (gamepause) {
//                    gc.pause();
//                    gamepause = false;
//                } else {
//                    gc.resume();
//                    gamepause = true;
//                }
//            }

            if (input.isKeyPressed(Input.KEY_ESCAPE)) {
                gameend = true;
            }
        }
        a.update(delta);

    }

}
